var loginURL = "http://192.168.1.102/Home/login";
$().ready(function() {
  /**过滤到特殊字符*/
	$("#j_username").each(function(){
	   $(this).blur(function(){
		   this.value= $.trim(this.value);
		   for(var i = 0; i < this.value.length; i++){
			   if(this.value.indexOf("*")>=0){
				   this.value=this.value.replace("*", "");
			   }
			   if(this.value.indexOf("%")>=0){
				   this.value=this.value.replace("%", "");
			   }
			   if(this.value.indexOf("=")>=0){
				   this.value=this.value.replace("=", "");
			   }
			   if(this.value.indexOf("(")>=0){
				   this.value=this.value.replace("(", "");
			   }
			   if(this.value.indexOf(")")>=0){
				   this.value=this.value.replace(")", "");
			   }
		   }
	   });
	});
  
  $('#j_username,#j_password,#j_checkcode,#j_new_password,#j_confirm_password').poshytip({
    className : 'tip-yellowsimple',
    showOn : 'none',
    alignTo : 'target',
    alignX : 'inner-left',
    offsetX : 50,
    offsetY : 5,
    fade : false,
    slide : true
  });
  $('#j_username,#j_password,#j_checkcode,#j_new_password,#j_confirm_password').each(function(){  
	   $(this).focus(function() {
		   $('#j_username,#j_password,#j_checkcode,#j_new_password,#j_confirm_password').each(function(){  
			   $(this).poshytip('hide');
			});
	    });
  });
  whoami();
});

function whoami() {
	$('#j_username').focus();
  var username = $.cookie('suid');
  if (username) {
    $('#j_username').val(username);
    $('#remember_me').attr('checked', 'checked');
  }
}
function validateField(id, regex, errormsg, trim) {
  var value = trim ? $.trim($(id).val()) : $(id).val();
  if(undefined == value){
    return true;
  }
  if (!regex.test(value)) {
    showMsg(id, errormsg);
    return false;
  }
  $(id).poshytip('hide');
  return true;
}

//校验指定id的两个值是否一致
function validateFieldConfirm(id, id2, errormsg, trim) {
	  var value = trim ? $.trim($(id).val()) : $(id).val();
	  var value2 = trim ? $.trim($(id2).val()) : $(id2).val();
	  if(undefined == value){
	    return true;
	  }
	  if (value != value2) {
	    showMsg(id, errormsg);
	    return false;
	  }
	  $(id).poshytip('hide');
	  return true;
	}

//显示提示信息
function showMsg(id, errormsg){
  $(id).poshytip('update', errormsg);
  $(id).poshytip('show');
}

// validate login fields
function validate() {
  var flag = validateField("#j_username", /.{1,100}$/, I18NMessage.usernameErrormsg, true);
  flag = flag && validateField("#j_password", /.{1,20}$/, I18NMessage.passwordErrormsg, false);
  flag = flag && validateField("#j_checkcode", /^[A-Za-z0-9]{4}$/, I18NMessage.checkcodeErrormsg, false);
  flag = flag && validateField("#j_new_password", /.{1,20}$/, I18NMessage.newpasswordErrormsg, false);
  flag = flag && validateFieldConfirm("#j_confirm_password", "#j_new_password", I18NMessage.equalpasswordErrormsg, false);
  if(flag){
	var data ={
		loginName:$("#j_username").val(),
		password:$("#j_password").val(),
		yzm:$("#j_checkcode").val(),
		yzmKey:window.yzmKey_random
	}
	$.post(loginURL,data,function(result){
		if(result.Status==true){
			window.location == "http://localhost:8080/admin/html/zxzp.html";
		}else{
			alert("账号或密码不正确");
			updateCheckCodeImg();
		}
	});
  }
}

/**  显示提示信息。*/
function setMsg(type, txt) {
  var divMsg = document.getElementById(type +"DivMsg");
  divMsg.className = "aui-message " + type;
  var span = document.createElement("span");
  span.className = "aui-icon icon-" + type;
  divMsg.appendChild(span);
  var p = document.createElement("p");
  p.innerHTML = txt;
  divMsg.appendChild(p);
}

/**
 * 检测是琐插入石化客户端USBKEY
 */
function checkSinopecUSBKEY(){
	//检测客户端证书
	/*
	var err = 0;
	try{
		var obj = new ActiveXObject("TassAuthClientX.kernal");
		var random = "sRandom";
		err = obj.IC_Sign("Tass Unite std RSA CSP Provider",random,32);
	} catch(objError) {
		return false;
	}
	if(err == 0) {
		return true;
	}
	*/
	return false;
}
